<template>
	<view class="flex-column">
		<view class="baseView" style="padding: 40rpx;">

			<view class="mainColor shadowView"
				style="margin-top: 20rpx; margin-bottom: 15rpx; font-size: 18px; font-weight: 500;">
				{{model.title}}
			</view>

			<view class="shadowView flex-column">
				<view class="blockTextColor" style="font-size: 16px;">
					资源简介：
				</view>
				<!--  -->
				<view class="subTextColor subText" style="margin-top: 20rpx; font-size: 16px;">
					{{model.description ?? '暂无简介'}}
				</view>
			</view>
			<!-- 资源链接 -->
			<view class="shadowView flex-column">
				<view class="blockTextColor" style="font-size: 16px;">
					资源链接：
				</view>
				<!--  -->
				<view class=" subText mainColor" style="margin-top: 20rpx; font-size: 18px; ">
					{{model.originalLinkContent}}
				</view>

				<view class="zsConter" style="margin-top: 20rpx">
					<view class="bottomBtn zsConter mainBgColor" style="margin-bottom: 20px;" @click="copyMethod()">
						复制链接
					</view>
				</view>

			</view>

			<view v-if="isBaiduLink()==true" class="zsConter" style="margin-top: 20rpx">
				<view class="bottomBtn zsConter mainBgColor" style="margin-bottom: 20px;"
					@click="openurl(model.extractLinks)">
					点击跳转百度网盘小程序保存
				</view>
			</view>
		</view>

            <!-- 分享按钮 -->
			<view style="margin: 30rpx 0rpx;" class="">
				<BtnsView @btnsClick='btnsClick'></BtnsView>
			</view>


			<!-- //弹窗 -->
			<uni-popup ref="popup" type="center">
				<shareMomentsView v-if="showSharePopup==true" @closeClick='this.$refs.popup.close()'
					:menuButtonInfo="menuButtonInfo"></shareMomentsView>
			</uni-popup>

	</view>
</template>

<script>
	import BtnsView from '../../components/btns-view/btns-view.vue'
	import shareMomentsView from '../../components/share-moments-view/share-moments-view.vue'
	import expressAdView from '../../components/express-ad-view/express-ad-view.vue'

	export default {
		components: {
			BtnsView,
			shareMomentsView,
			expressAdView
		},
		data() {
			return {
				seeAuthorQRcode: false, //是否查看作者
				shareImageUrl: 'https://mp-35bd1c1b-8095-44b6-b494-c65e6d85fd9c.cdn.bspapp.com/appConfigure/资源分享图.jpg',
				model: {},
				showSharePopup: false,
				menuButtonInfo: {}
			}
		},
		onReady() {
			// 在页面准备好时获取胶囊按钮的位置信息
			this.menuButtonInfo = uni.getMenuButtonBoundingClientRect();
			console.log('胶囊信息===', this.menuButtonInfo);
		},
		onLoad(options) {
			this.model = this.$decodeJson(options.model);
			uni.setNavigationBarTitle({
				title: this.model.title
			})
			this.copyMethod();


		},
		onShow() {

		},
		// 组件的methods部分
		methods: {
			isBaiduLink() {
				if(this.model.originalLinkContent == null){
					return false;
				}
				if (this.model.originalLinkContent.includes('baidu.com')) {
					return true;
				}
				return false;
			},
			copyMethod() {
				uni.setClipboardData({
					data: this.model.originalLinkContent,
					success: function() {
						uni.showToast({
							title: '链接已复制到剪切板',
							icon: 'success'
						});
					},
					fail: function(error) {
						uni.showToast({
							title: '复制失败',
							icon: 'none'
						});
						console.log('失败', error);
					}
				});
			},
			//看视频
			showAd() {
				if (this.ad) {
					this.ad.show().then(() => {
						console.log('激励视频广告显示成功');
					}).catch((err) => {
						console.error('激励视频广告显示失败', err);
					});
				}
			},
			btnsClick(data) {
				if (data.index == 1) {
					this.showSharePopup = true;
					this.$refs.popup.open('center')
				} else {
					uni.navigateTo({
						url: '../integral-core-page/integral-core-page'
					})
				}
			},
			getStrUrl(s) {
				var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
				var reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
				s = s.match(reg);
				return (s && s.length ? s[0] : null);
			},
			openurl(url) {
				let cjurl = this.getStrUrl(url) //净化过后的url
				//小程序跳转
				let tzurl = cjurl.split('/s/')[1].split('?pwd')[0] //取出来链接
				let pwd = cjurl.split('/s/')[1].split('?pwd=')[1] //取出来提取码
				console.log('url==', tzurl, 'pwd=', pwd);
				uni.navigateToMiniProgram({
					appId: 'wxdcd3d073e47d1742',
					path: `pages/netdisk_share/share?scene=${tzurl}%23${pwd}%238%230`,
					extraData: {
						foo: 'bar'
					},
					envVersion: 'release',
					success(res) {
						// 打开成功
					}
				})
			},
			getShareStr(senderFriend) {
				var path = 'pages/resource-details-page/resource-details-page?'
				var dataId = this.model._id;
				var str =`sharePush=1&dataId=${dataId}`
				if (senderFriend) {
					str =`${path}&sharePush=1&dataId=${dataId}`
				}
				console.log('getShareStr==', str)
				return str;
			},
			getShareImageUrl() {
				if (this.model.resourceImages.length > 0) {
					return this.model.resourceImages[0]
				}
				return this.shareImageUrl;
			}
		},
		onShareAppMessage(res) {
			var title = '免费资源【'+this.model.title+'】网盘链接点击提取'
			let data = {
				title: title, //转发标题
				path: this.getShareStr(true),
				imageUrl: this.getShareImageUrl(), //转发图片
				success: function(res) {
					console.log('分享成功', res);
					uni.showToast({
						title: '分享成功'
					})
					// 分享成功的处理逻辑
				},
			}
			return data
		},
		onShareTimeline() {
			var title ='免费资源'+this.model.title+'网盘链接点击提取'
			return {
				title: title,
				query: this.getShareStr(false),
				imageUrl: this.getShareImageUrl(),
				success: function(res) {
					console.log('分享成功', res);
					this.showSharePopup = false;
					this.$refs.popup.close()
					uni.showToast({
						title: '分享成功'
					})
					// 分享成功的处理逻辑
				},
				fail: function(err) {
					console.error('分享失败', err);
					// 分享失败的处理逻辑
				},
				complete: function(res) {
					console.log('分享结束', res);
					// 分享结束的处理逻辑
				} //转发图片
			}
		}


	}
</script>

<style lang="scss" scoped>
	.tipsView {
		color: rgb(224, 168, 99);
		font-size: 30rpx;
		border-radius: 8rpx;
		padding: 16rpx;
		background-color: rgba(224, 168, 99, 0.2);
		border: 1px solid rgb(224, 168, 99);
	}

	.baseView {
		// background-image: url('https://mp-35bd1c1b-8095-44b6-b494-c65e6d85fd9c.cdn.bspapp.com/appConfigure/topBgImage.png');
		background-repeat: no-repeat;
		/* 背景图片不重复 */
		background-position: center;
		/* 背景图片居中显示 */
		background-size: cover;
		/* 背景图片覆盖整个元素区域 */
	}

	.shadowView {
		padding: 30rpx;
		overflow: hidden;
		// border-radius: 8rpx;
		// box-shadow: 0rpx 0rpx 8rpx gainsboro;
	}

	.imageView {
		width: 110rpx;
		height: 110rpx;
		border-radius: 8rpx;
	}

	.bottomBar {
		background-color: white;
		position: fixed;
		left: 0rpx;
		bottom: 0rpx;
		width: 100%;
		height: 130rpx;
	}

	.bottomBtn {
		color: white;
		font-size: 30rpx;
		font-weight: bold;
		height: 80rpx;
		border-radius: 40rpx;
		width: 70%;
	}

	.subText {
		font-size: 15px;

	}
</style>